package org.tof.hotel.entity;

import org.apache.log4j.Logger;
import org.tof.hotel.dao.UserDaoImpl;
import org.tof.hotel.dto.UserDTO;

import java.sql.SQLException;

/**
 * Web user class
 */
public class User {
    static Logger logger = Logger.getLogger(User.class);
    private UserDTO dto;

    public User(String email, String password) {
        dto.setEmail(email);
        dto.setPassword(password);
    }

    /**
     * Flushes dto to the database
     *
     * @throws SQLException if a database access error occurs
     */
    public void flush() throws SQLException {
        //check if there's no such dto already in the database
        if (new UserDaoImpl().findByEmail(dto.getEmail()) != null) {
            //insert dto and assign id to it
            dto.setId(new UserDaoImpl().insert(dto));
        }
    }

    public static UserDTO findUserByEmail(String email){
        //email syntax is checked upon submitting

        UserDTO dto = null;
        try {
            dto = new UserDaoImpl().findByEmail(email);
        } catch (SQLException e) {
            logger.error(e.toString(), e);
        }
        return dto;
    }
}
